<template>
  <div class="dashboard-container">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-row style="height:60px">
            <el-button size="small" type="primary" icon="el-icon-plus" @click="showEditDlog = true">新增角色</el-button>
          </el-row>
          <el-table
            border
            style="width: 100%"
            :data="list"
          >
            <el-table-column
              type="index"
              label="序号"
              width="120"
            >
            </el-table-column>
            <el-table-column
              prop="name"
              label="名称"
              width="240"
            >
            </el-table-column>
            <el-table-column
              label="描述"
              prop="description"
            >
            </el-table-column>
            <el-table-column
              label="操作"
            >
              <template v-slot="{row}">
                <el-button type="infor" @click="assginPerm(row.id)">分配权限</el-button>
                <el-button type="primary" @click="editHandle(row.id)">修改</el-button>
                <el-button type="warning" @click="deleteHandle(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row type="flex" justify="center" align="middle" style="height: 60px">
            <el-pagination
              layout="prev, pager, next"
              :page-size="page.pagesize"
              :total="page.total"
              :current-page="page.page"
              @current-change="changePage"
            >
            </el-pagination>
          </el-row>
        </el-tab-pane>

        <!-- 公司信息 -->
        <el-tab-pane label="公司信息">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          >
          </el-alert>
          <el-form label-width="120px" style="margin-top:50px" :data="companyList">
            <el-form-item label="公司名称">
              <el-input v-model="companyList.name" disabled style="width:400px"></el-input>
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="companyList.companyAddress" disabled style="width:400px"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="companyList.mailbox" disabled style="width:400px"></el-input>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="companyList.remarks" type="textarea" :rows="3" disabled style="width:400px"></el-input>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 弹出层 -->
    <el-dialog
      :title="roleForm.id?'编辑角色':'新增角色'"
      :visible.sync="showEditDlog"
      width="50%"
      @close="closeEditHandle"
    >
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeEditHandle">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </span>
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 分配权弹层 -->
    <el-dialog title="分配权限" :visible.sync="showPermDialog" @close="btnPermCencel">
      <!-- 权限是一颗树 -->
      <!-- 将数据绑定到组件上 -->
      <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
      <!-- id作为唯一标识 -->
      <el-tree ref="treeRef" :data="permData" :props="defaults" node-key="id" default-expand-all show-checkbox check-strictly :default-checked-keys="selectedTreeRolesId" />
      <!-- 确定 取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCencel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { deleteRole, getCompanyById, getRole, getRoles, editRole, addRoles, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Setting',
  components: {},
  props: {},
  data() {
    return {
      list: [], // 列表角色数据
      page: {
        page: 1, // 页码
        pagesize: 2, // 每页条数
        total: 0 // 总条数
      },
      companyList: {},
      showEditDlog: false, // 是否显示编辑弹层
      roleForm: {
        name: '',
        desciption: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },
      showPermDialog: false, // 显示分配权限弹层
      permData: [], // 权限数据
      defaults: { label: 'name' },
      roleId: null, // 角色Id
      selectedTreeRolesId: [] // 选中权限
    }
  },
  created() {
    this.getRoles()
    this.getCompanyById()
  },
  mounted() {},
  methods: {
    // 分配权限事件
    async assginPerm(id) {
      this.roleId = id
      const { permIds } = await getRole(this.roleId)
      // console.log('data', data)
      this.selectedTreeRolesId = permIds
      const list = await getPermissionList()
      this.permData = transListToTreeData(list, '0')
      this.showPermDialog = true
    },
    // 权限提交
    async  btnPermOK() {
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.treeRef.getCheckedKeys()
      })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    // 取消权限弹层
    btnPermCencel() {
      this.showPermDialog = false
    },
    // 请求获取所有角色列表
    async getRoles() {
      const { total, rows } = await getRoles(this.page)
      this.list = rows
      this.page.total = total
    },
    // 页码跳转
    changePage(val) {
      // 改变对象的页码
      this.page.page = val
      // 重新传入最新页码 发起请求
      this.getRoles()
    },
    // 获取公司信息
    async getCompanyById() {
      const data = await getCompanyById(this.$store.state.user.userInfo.companyId)
      this.companyList = data
    },
    // 删除事件
    deleteHandle(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        console.log('确定')
        await deleteRole(id)
        // 提示删除
        this.$message.success('删除成功!')
        this.getRoles()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {

      })
    },
    // 点击编辑按钮弹出层获取角色信息
    async editHandle(id) {
      this.roleForm = await getRole(id)
      this.showEditDlog = true
    },

    // 提交角色事件
    async btnOK() {
      this.$refs.roleForm.validate().then(async action => {
        if (this.roleForm.id) { // 判断是否有id 有id为编辑 没id为添加
          // 修改角色
          await editRole(this.roleForm)
          this.$message.success('编辑成功!')
        } else {
          // 新增角色
          await addRoles(this.roleForm)
          this.$message.success('添加成功!')
        }
        this.showEditDlog = false
        this.getRoles()
      })
    },
    // 关闭弹层事件
    closeEditHandle() {
      this.roleForm = this.$options.data().roleForm
      this.$refs.roleForm.resetFields()
      this.showEditDlog = false
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
